<template>
  <div class="login-container">

    <div style="height: 100%;">
      <div class="background" style="position:absolute;top: 40px;left:40px;overflow:hidden;height:calc( 100% - 80px );width:calc( 100% - 80px ); ">
        <div style="height:calc( 100% + 80px ) ;width:calc( 100% + 80px )">
          <img src="@/assets/login/background_up.png" style="padding:0;margin:0;margin-left:-40px;margin-top:-40px;width: 100%;height: 100%;display: block;" alt="">
        </div>
      </div>
      <!-- <a href="/" class="logo-version">
			<img src="{$_W['attachurl']}{$data['logo']}" class="logo">
			<span class="version hidden">{IMS_VERSION}</span>
		</a> -->
      <div style="position: absolute;top: 40px;left:40px;overflow:hidden;height:calc( 100% - 80px );width:calc( 100% - 80px ); display: flex;">
        <div style="height: 100%;flex: 9;position:relative;">
          <a href="" style="position: absolute;left:40px;top:40px;"><img src="@/assets/login/logo.png" alt=""></a>
          <div style="position: absolute;right:80px;bottom:50px;text-align: right;"><img src="@/assets/login/user_management_background.png" style="width: 80%;" alt=""></div>
        </div>

        <div class="" style="flex:4;background:white;display: flex;align-items: center;justify-content: center;">
          <div>
            <div class="title form-input"><img src="@/assets/login/welcome.png" alt=""></div>
            <form id="form1" action="" method="post" role="form" onsubmit="return formcheck();" class="we7-form">
              <div class="input-group-vertical form-input">
                <input name="username" type="text" class="form-control " placeholder="输入登录账号">
                <input name="password" type="password" class="form-control password" placeholder="输入登录密码">
              </div>
              <!-- <div class="checkbox form-input">
                                <input type="checkbox" value="true" id="rember" name="rember">
                                <label for="rember">记住用户名</label>
                            </div> -->
              <div class="login-submit text-center form-input">
                <input id="submit" type="submit" style="height: 50px;" name="submit" value="登录" class="btn btn-primary btn-block">
                <div class="text-right" />
                <input name="token" value="{$_W['token']}" type="hidden">
              </div>
            </form>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'
import { getWordList } from '@/api/folder'

export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error('Please enter the correct user name'))
      // } else {
      callback()
      // }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('The password can not be less than 5 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      // loginRules: {
      //   username: [
      //     { required: true, trigger: "blur", validator: validateUsername },
      //   ],
      //   password: [
      //     { required: true, trigger: "blur", validator: validatePassword },
      //   ],
      // },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      disabled: true,
      isLogin: false,
      disBtn: true,
      timer: '',
      count: 0,
      otherQuery: {},
      show: false,
      html: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    },
    isLogin: {
      handler: function(flag) {
        if (!flag) {
          this.disabled = true
        }
      }
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    // i = 0;
    // i++;
    // if (i == 1) {
    //   location.reload();
    // }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    confirmLogin() {
      this.isLogin = true;
      (this.showDialog = false), (this.disabled = false)
    },
    beforeLogin() {
      this.showDialog = true
      new Promise((resolve, reject) => {
        getWordList().then((response) => {
          this.html = response.data[0].html
          this.count = 5
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count < 6) {
              this.count--
            } else {
              this.disBtn = false
              this.show = true
              clearInterval(this.timer)
              this.timer = null
            }
          }, 1000)
        })
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (this.loginForm.username == '') {
        this.$message({
          message: '请填写用户名!',
          type: 'error'
        })
        return
      }
      if (this.loginForm.password == '') {
        this.$message({
          message: '请填写密码!',
          type: 'error'
        })
        return
      }
      if (this.isLogin == false) {
        this.$message({
          message: '登录前请先同意须知!',
          type: 'error'
        })
        return
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((response) => {
              this.$router.push('/')
              this.loading = false
              this.$message({
                message: '登录成功',
                type: 'success'
              })
            })
            .catch(() => {
              // console.log("我catch了");
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
        }
        return acc
      }, {})
    }
  }
}
</script>

<style>
.login-container {
  width: 100%;
  height: 100%;
  background-image: url("../../assets/loginBg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  z-index: 0;
  padding: 5% 10% 5% 10%;
  display: flex;
  align-items: center;
}

.login-container .login-center {
  width: 960px;
  height: 450px;
  margin: 0 auto;
  position: relative;
  z-index: 1;
  /* height: 100%; */
  border-radius: 5px;
  display: flex;
  flex-direction: row;
  /* border:red 1px solid; */
  background: white;
}
.login-container .login-center .rightImg {
  width: auto;
  right: 0;
  top: 0;
  height: 70%;
  position: absolute;
}
.login-container .center-left {
  width: 43%;
  /* border: 1px solid red; */
  height: 100%;
}
.login-container .center-right {
  width: 57%;
  /* border: 1px solid red; */
  height: 100%;
  padding: 7% 10% 7% 10%;
  /* padding: 10%; */
  z-index: 3;
}
.login-container .center-left-head {
  width: 100%;
  height: auto;
  /* border: 1px solid red; */
  /* align-items: center; */
  padding: 30px 0px 0px 50px;
}

.login-container .center-right .form {
  /* border: 1px solid red; */
  width: 100%;
  height: 100%;
}
.login-container .center-right .form .login-form {
  width: 100%;
  height: 100%;
  /* padding: 10%; */
}
.login-container .title-container {
  text-align: center;
  letter-spacing: 1px;
  margin-bottom: 60px;
  color: #281b6d;
}
.login-container .center-right .form .el-button {
  background: #281b6d;
  border: #281b6d;
  color: white;
  font-size: 14px;
  letter-spacing: 3px;
  font-weight: bold;
}
.login-container .center-right .form .el-button:hover {
  background: #281b6d;
  opacity: 0.7;
  border: #281b6d;
  color: white;
  font-size: 14px;
  letter-spacing: 3px;
  font-weight: bold;
}
.login-container .center-right .form .el-input__inner {
  border: none;
  border-bottom-width: 2px;
  border-bottom-style: ridge;
  border-radius: 0px;
  padding-bottom: 2px;
  background-color: transparent;
}
.center-right .form .el-input {
  margin-bottom: 10px;
}
.login-container .notice {
  margin: 0;
  display: flex;
  flex-direction: row;
  justify-content: baseline;
  margin-top: 10px;
}
.login-container .notice .notice_checkbox {
  width: 20px;
  height: 20px;
  font-size: 16px;
}
.login-container .notice .notice_text {
  font-size: 15px;
  color: rgb(85, 84, 84);
  padding-left: 3px;
}
.login-container .confirmText {
  height: 300px;
  overflow: scroll;
  overflow-x: hidden;
}
.login-container .el-dialog__footer {
  padding-top: 0px;
}
</style>
